#include "core/syscall.h"
#include "core/task.h"
#include "applib/applib.h"
#include "tools/log.h"
#include "fs/fs.h"
#include "core/memory.h"
void sys_printmsg(const char* msg,int arg)
{
	print_log(msg,arg);
}

//函数指针
typedef int (*syscall_handler_t)(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3);

static const syscall_handler_t  sys_table[] = {
    [SYS_sleep] = sys_sleep,
	[SYS_getpid] = sys_getpid,
	[SYS_printmsg] = sys_printmsg,
	[SYS_fork] = sys_fork,
	[SYS_execve] = sys_execve,
	[SYS_yield] =  sys_sched_yield,
	[SYS_open] = sys_open,
	[SYS_read] = sys_read,
	[SYS_write] = sys_write,
	[SYS_close] = sys_close,
	[SYS_lseek] = sys_lseek,
	[SYS_isatty] = sys_isatty,
	[SYS_sbrk] = sys_sbrk,
	[SYS_fstat] = sys_fstat,
	[SYS_dup] = sys_dup,
	[SYS_exit] = sys_exit,
	[SYS_wait] = sys_wait,

	[SYS_opendir] = sys_opendir,
	[SYS_readdir] = sys_readdir,
	[SYS_closedir] = sys_closedir,
	[SYS_ioctl] = sys_ioctl,

	[SYS_unlink] = sys_unlink,
};

void  do_handler_syscall(syscall_frame_t* frame)
{
     if (frame->func_id < sizeof(sys_table) / sizeof(sys_table[0])) 
	 {
		// 查表取得处理函数，然后调用处理
		syscall_handler_t handler = sys_table[frame->func_id];
		if (handler) 
		{
			int ret = handler(frame->arg0, frame->arg1, frame->arg2, frame->arg3);
            frame->eax = ret;
			return;
		}
	}
	// 不支持的系统调用，打印出错信息
	task_t * task = cur_task();
	print_log("task: %s, Unknown syscall: %d", task->name,  frame->func_id);
}

